package com.nongfuspring.nongfu.service;

import com.nongfuspring.nongfu.mapper.UserMapper;
import com.nongfuspring.nongfu.pojo.Opinion;
import com.nongfuspring.nongfu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userMapper.findUserByName(username);
    }

    public List<User> getUsers(int pageSizes,int pageNum) {
        return userMapper.getUsers(pageSizes,(pageNum-1)*pageSizes);
    }

    public Integer updateUser(User user) {
        return userMapper. updateUser( user);
    }

    public Integer setRoleForUser(User user) {
        String userid =userMapper.getcardnumber(user.getCardNumber());
        if(user.getLevel().equals("admin")){
            return userMapper. setRoleForUser(Integer.valueOf(userid),1);
        }else{
            return userMapper. setRoleForUser(Integer.valueOf(userid),2);
        }

    }

    public Integer addOpinion(Opinion opinion) {
        return userMapper.addOpinion(opinion);
    }

    public Integer getTotalUsers() {
        return userMapper.getTotalUsers();
    }
}
